"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFSkCtrl
 * @description
 * # FinanceFSkCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "FinanceFSkCtrl",
    function($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
      var id = ($scope.process_id = $routeParams.id);
      $scope.getpaytype = function() {
        var param = {};
        param.type = "收款类型";
        Util.getKeyValue(param, function(data) {
          $scope.payTypeList = data.data;
        });
      };
      $scope.choosePeople = function() {
        var relModuleObj = {
          name: "FinanceFSkCtrl",
          selectTab: "select_user"
        };
        // 清掉关联的编号
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);

        $location.path("finance/choosepeople");
      };
      $scope.showCertificate = function() {
        var param = {};
        param.type = "收款类型";
        Util.getKeyValue(param, function(data) {
          $scope.resiTypeList = data.data;
          $rootScope.$emit("showmasker");
          $scope.showCerPicker = true;
        });
      };
      $scope.deletedItem = function(index) {
        $scope.param.detailKips.splice(index, 1);
        $scope.index = -1;
      };
      $scope.addCertificate = function() {
        $scope.picker = $scope.picker || {};
        var check = false;
        check = checkPicker($scope.picker);
        if (!check) {
          return;
        }
        $scope.param.detailKips.push($scope.picker);
        // 隐藏表单
        hideCertificate();
      };
      var hideCertificate = function() {
        $scope.picker = {};
        $rootScope.$emit("closemasker");
        $scope.showCerPicker = false;
      };
      $scope.$on("closemasker", function() {
        hideCertificate();
      });
      $scope.cacelCertificate = function() {
        hideCertificate();
      };
      $scope.swipe = function(index) {
        $scope.index = index;
      };
      $scope.resetlist = function(index) {
        $scope.index = -1;
      };
      // 验证是否输入完整
      var checkPicker = function(data) {
        var check = true;
        if (!data.name) {
          Util.Toast("请选择收款类型");
          check = false;
          return check;
        }
        if (!data.value) {
          Util.Toast("请填写金额");
          check = false;
          return check;
        }
        return check;
      };
      $scope.submit = function(operationId) {
        $scope.param.details = angular.toJson($scope.param.detailKips);
        // 底部类型end
        var options = {};
        // 流程id
        options.id = id;
        // 根据operationId判断是草稿还是提交
        options.operationId = operationId;
        options.urlParam = openApi.otherMoney;
        options.param = angular.copy($scope.param, {});
        delete options.param.detailKips;
        FormSubmit.submit(options,$scope);
      };
      $scope.validate = function() {
        if (!$scope.param.payPerson) {
          Util.Toast("请选择交款人");
          return;
        }
        if (!($scope.param.payTypeArr && $scope.param.payTypeArr.length)) {
          Util.Toast("请选择收款类型");
          return;
        }
        $scope.param.payType = $scope.param.payTypeArr.toString();
        if (!$scope.param.amount) {
          Util.Toast("请输入收款金额");
          return;
        }
        if (!$scope.param.remark) {
          Util.Toast("请填写收款事由");
          return;
        }
        if (
          $scope.param.relatedModuleName &&
          $scope.param.relatedModuleName !== "无"
        ) {
          if (!$scope.param.moduleNo) {
            Util.Toast("请选择模块编号");
            return;
          }
        }
        if (!$scope.validation.passvalidation) {
          Util.Toast($scope.validation.tip);
          return;
        }
        return true;
      };

      var init = function() {
        // 获取回显的详细信息
        Util.getDetailData(id, function(data) {
          $scope.param = data;
          if ($scope.param.payType) {
            $scope.param.payTypeArr = $scope.param.payType.split(",");
          }
          $scope.param.detailKips = $scope.param.detailKips || [];
        });
        $scope.getpaytype();
      };
      // 初始化
      init();
    }
  );
